#include <windows.h>
#include <gl/gl.h>
#include "GlButton.h"
#include "TextureManager.h"


using namespace std;

GlButton::GlButton(void) : _isMouseOver (false)
{
}

GlButton::~GlButton(void)
{
}

void GlButton::_onMouseIn (const MouseEvent& mouseEvent)
{
  _isMouseOver = true;
}

void GlButton::_onMouseOut (const MouseEvent& mouseEvent)
{
  _isMouseOver = false;
}

void GlButton::_render () const
{

  try {
    if (_isMouseOver)
      glBindTexture (GL_TEXTURE_2D,
        TextureManager::getSingleton ().getTexture (_mouseOverTexture));
    else
      glBindTexture (GL_TEXTURE_2D,
        TextureManager::getSingleton ().getTexture (getBackgroundTex ()));
  } catch (BaseException* e) {
    glBindTexture (GL_TEXTURE_2D, 0);
  }

  glEnable (GL_TEXTURE_2D);
  glBegin (GL_QUADS);
  glTexCoord2f (0.0f, 1.0f); glVertex2f (getX (), getY ());
  glTexCoord2f (0.0f, 0.0f); glVertex2f (getX (), getY () + getHeight ());
  glTexCoord2f (1.0f, 0.0f); glVertex2f (getX () + getWidth (), getY () + getHeight ());
  glTexCoord2f (1.0f, 1.0f); glVertex2f (getX () + getWidth (), getY ());
  glEnd ();
  glDisable (GL_TEXTURE_2D);

  glBindTexture (GL_TEXTURE_2D, 0);
}
